<?php
class ProductAction extends CommAction {
	function index(){

		import("ORG.Util.Page");
		
		$pro = D('product_view');
		$count = $pro->count();
		$p  = new Page ($count, 10); 

		$p->setConfig('header','篇记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');		
		$url = "Product:product_list";

		if(!empty($_GET['cid'])){
			$condition = "cid = ".$_GET['cid'];
		}
		elseif(!empty($_GET['pid'])){
			
			$condition = "pid = '".$_GET['pid']."'";
			$url = "Product:product_content";
			$this->assign ( "nowp", $_GET['nowp']);
		}
		else{
			$condition = '1';
		}
		
        $list = $pro->where($condition)->order('ctime desc')->limit($p->firstRow.','.$p->listRows)->findAll(); 
		
		$page = $p->show (); 		
		
		$this->assign ( "cid", $cid );
		$this->assign ( "page", $page );
        $this->assign ( "list", $list );
        $this->assign ("current_p",$p->nowPage);
        $this->display($url);			
		
	}
	
	function product_add(){
		
		if($_POST['product_bt']){

			
			if($_POST['pattern']){
				$pro_arr['cid'] = $_POST['cate_tree'];
				$pro_arr['pattern'] = $_POST['pattern'];
				$att_arr = $this->_upload("Product",false,360,240,true);

				
				//dump($att_arr);
				$pro_arr['pic'] = $att_arr['pic'];
				$pro_arr['sample'] = $att_arr['sample'];

				

				$pro_arr['content'] = stripslashes($_POST['content']);
				$pro_arr['ctime'] = time();
				
				
				if(d("product")->add($pro_arr)){
						//echo d("article")->getlastsql();

					$this->assign('jumpUrl',"__APP__/product"); 
					$this->success("添加产品成功!");
				}
				
				
			}
			else{

			}

			
		}
		else{
			$cate = d('category')->findall();
			$this->assign('cate_list',$cate); 			
			$this->display("Product:product_add");
		}
	}

	function product_edit(){
		
		if($_POST['product_bt']){

			

			if($_POST['pattern']){
				
				if($_POST['pid']){
					$pro_arr['cid'] = $_POST['cate_tree'];
					$pro_arr['pattern'] = $_POST['pattern'];
					
					$att_arr = $this->_upload("Product",false,360,240,true);

					if(!empty($att_arr['pic'])){
						$pro_arr['pic'] = $att_arr['pic'];
						
					}	
					if(!empty($att_arr['sample'])){
						$pro_arr['sample'] = $att_arr['sample'];
					}

					
					$pro_arr['content'] = stripslashes($_POST['content']);
					$pro_arr['ctime'] = time();
					
					
					
					if(d("product")->where("pid = '".$_POST['pid']."'")->save($pro_arr)){						

						$this->assign('jumpUrl',"__APP__/product"); 
						$this->success("修改产品成功!");
					}
					else{
						$this->assign('jumpUrl',"__APP__/product"); 
						$this->success("修改产品失败,产品id为空!");						
					}
			    
				}
				else{
						$this->assign('jumpUrl',"__APP__/product"); 
						$this->success("修改产品失败,产品id为空!");
				}
				//echo d("article")->getlastsql();
				
			}
			else{
				$this->assign('jumpUrl',"__APP__/product");
			}

			
		}
		else{

			if($_GET['pid']){

				$cate = D('category')->findall();

				$pro = D('product_view')->where("pid = '".$_GET['pid']."'")->find();
				
				//dump($pro);
				$this->assign('cate_list',$cate);
				$this->assign('product',$pro);
				$this->display("Product:product_edit");
			}
			else{
				$this->assign('jumpUrl',"__APP__/product");
			}
		}
	}



	

}

?>